using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.ServiceModel.Syndication;
using ServiceModel.Syndication.LEAP2A;

namespace LeapExamples {
    public partial class Import : Form {
        private LeapFeed feed;
        private Dictionary<string, bool> done;

        public Import() {
            InitializeComponent();
        }

        private void importBtn_Click(object sender, EventArgs e) {
            using (StringReader sr = new StringReader(xml.Text)) {
                using (XmlTextReader xtr = new XmlTextReader(sr)) {
                    try {
                        feed = LeapFeed.Load<LeapFeed>(xtr);
                    } catch {
                        MessageBox.Show("Couldn't parse feed");
                        return;
                    }
                }
            }

            done = new Dictionary<string, bool>();
            treeView.Nodes.Clear();

            foreach (LeapItem entry in feed.Items) {
                ProcessEntry(entry, treeView.Nodes, true);
            }
        }

        private void ProcessEntry(LeapItem entry, TreeNodeCollection nodes, bool topLevel) {
            if (done.ContainsKey(entry.Id)) {
                return;
            }

            if (LeapHelpers.IsAsset(feed, entry) || !topLevel) {
                string title = "";

                done.Add(entry.Id, true);
                    
                title = entry.LeapType + " : " + LeapHelpers.GetAtomText(entry.Title);

                if (entry.FindLeapCategory(LeapCategorySchemes.SelectionType) == LeapSelectionTypeTerms.Webpage) {
                    title += "Webpage";
                }

                TreeNode newNode = nodes.Add(title);
                newNode.ToolTipText = LeapHelpers.GetAtomContent(entry.Content);

                ProcessChildren(entry, LeapPredicates.HasPart, newNode.Nodes);
                ProcessComments(entry, newNode.Nodes);
            }
        }

        private void ProcessChildren(LeapItem entry, LeapPredicates linkType, TreeNodeCollection nodes) {
            IList<LeapItem> linked = entry.GetLinkedByLinkType(linkType);

            foreach (LeapItem part in linked) {
                ProcessEntry(part, nodes, false);
            }
        }

        private void ProcessComments(LeapItem entry, TreeNodeCollection nodes) {
            IEnumerable<LeapItem> comments = entry.Comments;

            foreach (LeapItem comment in comments) {
                ProcessEntry(comment, nodes, false);
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            xml.Text = "";
            treeView.Nodes.Clear();
        }

        private void button2_Click(object sender, EventArgs e) {
            Export.ExportExample();
        }
    }
}